package uk.ac.jisc.depositmo.widgets;

/**
 * Copyright (c) 2011, University of Southampton
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * # Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer.
 * # Redistributions in binary form must reproduce the above copyright notice, 
 *     this list of conditions and the following disclaimer in the documentation 
 *     and/or other materials provided with the distribution.
 * # Neither the name of the University of Southampton nor the names of its
 *     contributors may be used to endorse or promote products derived from this
 *     software without specific prior written permission.
 *  
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

/**
 * 
 * @author Mark Borkum <mark.borkum@gmail.com>
 *
 */
public class DepositDialog {
	private static final boolean ALWAYS_CONFIRM_CLOSE = false;
	
	private Shell shell;
	
	private FileTransferTable fileTransferTable;
	
	private DepositForm depositForm;
	
	private Button submitButton;
	
	public DepositDialog(Shell parent) {
		this(parent, new File[0]);
	}
	
	public DepositDialog(Shell parent, File[] files) {
		/**
		 * 1. Construct widgets.
		 */
		
		final Shell shell = new Shell(parent, SWT.SHELL_TRIM);
		shell.setText("New Deposit");
		
		this.setShell(shell);
		
		final FileTransferTable fileTransferTable = new FileTransferTable(shell, files) {
			@Override
			public void filesModified() {
				shell.setMinimumSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT));
				
				setEnabledSubmitButton();
			}
		};
		
		this.setFileTransferTable(fileTransferTable);
		
		final DepositForm depositForm = new DepositForm(shell) {
			@Override
			public void fieldModified() {
				setEnabledSubmitButton();
			}
		};
		
		this.setDepositForm(depositForm);
		
		final Button submitButton = new Button(shell, SWT.PUSH);
		submitButton.setText("Deposit Files");
		
		shell.setDefaultButton(submitButton);
		
		this.setSubmitButton(submitButton);
		
		/**
		 * 2. Hook listeners.
		 */
		
		shell.addListener(SWT.Close, new Listener() {
			public void handleEvent(Event e) {
				if ((fileTransferTable.getFileCount() == 0) && !ALWAYS_CONFIRM_CLOSE) {
					e.doit = true;
				} else {
					final MessageBox messageBox = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO);
					
					messageBox.setMessage("Are you sure you want to cancel this deposit?");
					messageBox.setText("Cancel Deposit");
			        
			        e.doit = (messageBox.open() == SWT.YES);
				}
			}
		});
		
		submitButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				depositFiles();
			}
		});
		
		/**
		 * 3. Set layout.
		 */
		
		shell.setLayout(new GridLayout());
		
		fileTransferTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		
		depositForm.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
		
		submitButton.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
		
		shell.setMinimumSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		
		shell.pack();
		
		/**
		 * 4. Clean up.
		 */
		
		this.setEnabledSubmitButton();
	}
	
	public boolean isValid() {
		final FileTransferTable fileTransferTable = this.getFileTransferTable();
		final DepositForm depositForm = this.getDepositForm();
		
		if ((fileTransferTable != null) && (depositForm != null)) {
			return (fileTransferTable.getFileCount() > 0) && depositForm.isValid();
		} else {
			return false;
		}
	}
	
	private void setEnabledSubmitButton() {
		final Button submitButton = this.getSubmitButton();
		
		if (submitButton != null) {
			submitButton.setEnabled(isValid());
		}
	}

	private void setSubmitButton(Button submitButton) {
		this.submitButton = submitButton;
	}

	public Button getSubmitButton() {
		return submitButton;
	}

	private void setFileTransferTable(FileTransferTable fileTransferTable) {
		this.fileTransferTable = fileTransferTable;
	}

	public FileTransferTable getFileTransferTable() {
		return fileTransferTable;
	}

	private void setDepositForm(DepositForm depositForm) {
		this.depositForm = depositForm;
	}

	public DepositForm getDepositForm() {
		return depositForm;
	}

	private void setShell(Shell shell) {
		this.shell = shell;
	}

	public Shell getShell() {
		return shell;
	}
	
//	public void open() {
//		final Shell shell = this.getShell();
//		
//		if (shell != null) {
//			shell.open();
//		}
//	}
	
	public boolean depositFiles() {
		if (isValid()) {
			MessageBox messageBox = new MessageBox(getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
			
			messageBox.setMessage("Are you sure you want to perform this deposit?");
			messageBox.setText("Confirm Deposit");
	        
			if (messageBox.open() == SWT.YES) {
				return reallyDepositFiles();
			}
		} 
		
		return false;
	}
	
	private boolean reallyDepositFiles() {
		final MessageBox messageBox = new MessageBox(getShell(), SWT.ICON_INFORMATION | SWT.OK);
		
		messageBox.setMessage("This is a demonstration. The files were NOT deposited at the specified URL. Thank you for sharing your username and password!");
		messageBox.setText("Thank You");
        
        messageBox.open();
        
        // Call "dispose" instead of "close" to skip the confirmation message.
        getShell().dispose();
        
        return true;
	}
}
